package com.controller;

import com.anno.FrontLogin;
import com.controller.base.BaseController;
import com.controller.base.Response;
import com.model.FrontUser;
import com.model.FrontUserLocation;
import com.service.FrontUserLocationService;
import com.utils.from.FrontUserLocationForm;
import io.swagger.annotations.Api;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.time.LocalDateTime;


@RestController
@RequestMapping("/frontUserLocation")
@Api("前端用户地址接口")
public class FrontUserLocationController extends BaseController {

    @Resource
    private FrontUserLocationService service;

    @FrontLogin
    @PostMapping("/getLocationData")
    public Response getLocationData(HttpSession session) {
        FrontUser user = (FrontUser) session.getAttribute("front_user");
        FrontUserLocation userLocation = service.findOneByUserId(user.getId());
        return success(userLocation);
    }

    @FrontLogin
    @PostMapping("/update")
    public Response update(@Valid FrontUserLocationForm form, HttpSession session) {
        FrontUser user = (FrontUser) session.getAttribute("front_user");
        FrontUserLocation userLocation = new ModelMapper().map(form, FrontUserLocation.class);
        userLocation.setUserId(user.getId());
        userLocation.setUpdateTime(LocalDateTime.now());
        service.setSaveMap(userLocation);
        return success();
    }
}
